<?php
namespace Home\Controller;

use \Think\Controller;

class ExploreController extends HomeController
{
    public function index()
    {
        // 判断分页条件
        if(!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        } else {
            $p = 1;
        }

        // 判断展示模式
        if(!empty($_GET['type'])) {
            $type = $_GET['type'];
            if ( !in_array($type, array('question','article'))){
                $type = 'question';
            }
        } else {
            $type = 'question';
        }

        // var_dump($type);
        // 释放类型
        unset($_GET['type']);
        unset($_GET['p']);

        switch ($type) {

            case 'question':
                $data = M('question')->field('id qid, name qname,user_id quid, user_name quname, answer_num qanum')->where(array('display'=>1))->order('answer_num DESC')->page($p, 10)->select(); //每页10条

                if (!empty($data)) {
                    $i = 0;
                    foreach ($data as $val) {
                        // 循环内尽量不做查询
                        // 但是不一定所有问题都有答案,如果关联查询,则无答案的问题会被舍去
                        $arr = M('answer')->field('id aid, part apart, zan azan, discuss_number adnum, datetime atime')->where('question_id='.$val['qid'])->limit(0,1)->order('discuss_number DESC')->select();
                        // var_dump($arr);
                        $data[$i]['aid'] = $arr[0]['aid'];
                        $data[$i]['apart'] = $arr[0]['apart'];
                        $data[$i]['azan'] = $arr[0]['azan'];
                        $data[$i]['adnum'] = $arr[0]['adnum'];
                        $data[$i]['atime'] = $arr[0]['atime'];
                        $i++;
                    }
                }
                // var_dump($topicInfo);
                // var_dump($data);
                break;

            case 'article':
                // 查询文章信息
                $ar = M('article');
                $data = $ar->field('ar.id arid, ar.filename arfile, ar.title artitle, ar.image arimage, ar.discuss_number ardnum, ar.datetime artime, u.id uid, u.image uimage, u.name uname')->table('zhihu_article ar, zhihu_user u')->where('ar.user_id=u.id AND ar.display=1')->order('ar.discuss_number DESC')->page($p, 10)->select();

                for ($i=0; $i < count($data); $i++) {
                    //拼接路径
                    $path = './Data/'.substr($data[$i]['arfile'], 0, 4).'/'.substr($data[$i]['arfile'], 4, 2).'/'.substr($data[$i]['arfile'], 6, 2).'/'.$data[$i]['arfile'];
                    // 获取文件的一部分来展示
                    $data[$i]['part'] = mb_substr(strip_tags(html_entity_decode(file_get_contents($path, null, null, 0, 900))), 0, 240) ;
                }
                break;
        }

        // var_dump($data);
        // 页面需求数据
        $this->assign('title', '发现 - 知乎');
        $this->assign('p', $p+1);
        $this->assign('type', $type);
        $this->assign('data', $data);
        $this->display('Explore/index');

    }


}